package com.service;

import com.dao.userdao.Login;
import com.dao.userdao.PersonalCenter;
import com.pojo.Author;
import com.pojo.FansItem;
import com.pojo.FollowItem;
import com.pojo.User;
import com.util.MD5Util;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理个人中心的请求
 */
public class PersonalCenterService {
    /***
     * 获取用户基本信息
     */
      public static User get_user_info(String userid) throws IOException {
               User user=new User();
                user.setMailbox(userid);
                user= PersonalCenter.get_user_info(user);
               return user;
      }

    /**
     * 获取用户成就
     */
      public static User get_user_success(String email)throws IOException{
               return PersonalCenter.get_user_success(email);
      }

    /**
     * 验证旧密码
     */
    public static boolean verify_pwd(String pwd,String email)throws IOException{
           MD5Util md=new MD5Util();
           return PersonalCenter.judge_pwd(md.digest(pwd),email);
    }

    /***
     * 修改个人基本信息
     */
      public static void change_user_info(User user) throws IOException {
              PersonalCenter.update_user_info(user);
      }

    /***
     *修改密码
     */
      public static void change_user_pwd(String userid,String pwd) throws IOException {
              User user=new User();
              MD5Util md=new MD5Util();
              user.setPassword(md.digest(pwd));
               String is_mailbox="^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
               if(userid.matches(is_mailbox))
               {
                   user.setMailbox(userid);
               }else{
                   user.setUsername(userid);
               }
               PersonalCenter.update_user_info(user);
      }

    /**
     * 获取关注列表
     */
    public static List<FollowItem> get_following_list(String userid) throws IOException {
             return PersonalCenter.get_following_list(userid);
    }

    /**
     * 取消关注
     */
     public static void cancel_concern(HttpServletRequest req)throws IOException{
            String userid=req.getParameter("email");
            String following_id=req.getParameter("following_id");
            PersonalCenter.cancel_concern(userid,following_id);
     }

    /**
     * 账号注销
     */
    public static void account_logout(String userid)throws IOException{
           System.out.println("[info]注销账号");
           PersonalCenter.account_logout(userid);
    }

    /**
     *关注
     */
    public static void concern(HttpServletRequest req)throws IOException{
                 String userid= req.getParameter("email");
                 String following_id=req.getParameter("following_id");
                 PersonalCenter.concern(userid,following_id);
    }

    /**
     * 获取粉丝列表
     */
    public static List<FansItem> get_fans_list(String userid)throws IOException{
             return PersonalCenter.get_fans_list(userid);
    }

    /**
     * 获取作者信息
     */
     public static Author get_author_info(HttpServletRequest req)throws IOException{
           String author_id=req.getParameter("author_id");
           String self_id=req.getParameter("self_id");
           return PersonalCenter.get_author_info(author_id,self_id);
     }


}
